import { defineStore } from 'pinia'
import { store } from '/@/store'

export interface ModuleParam {
  id: number
  name: string
  desc: string
  app: string
  space: string
  type: string
  parent: number
}

interface ModuleState {
  parent: ModuleParam
  root: ModuleParam
}

export const useModuleStore = defineStore({
  id: 'module',
  state: (): ModuleState => ({
    parent: {
      id: 0,
      name: 'none',
      desc: '',
      app: '',
      space: '',
      type: '',
      parent: 0,
    },
    root: {
      id: 0,
      name: 'none',
      desc: '',
      app: '',
      space: '',
      type: '',
      parent: 0,
    },
  }),
  getters: {
    getParent(state): ModuleParam {
      return state.parent
    },
    getRoot(state): ModuleParam {
      return state.root
    },
  },
  actions: {
    setParent(param: ModuleParam) {
      this.parent = param
    },
    setRoot(param: ModuleParam) {
      this.root = param
    },
  },
})

// Need to be used outside the setup
export function useModuleStoreWithOut() {
  return useModuleStore(store)
}
